import http from "@/utils/request";

// 获取用户信息
export const getUserInfoApi = async () => {
  try {
    const res = await http.get("/user/info/get");
    if (res && res.code === 200 && res.data) {
      const userInfo = JSON.parse(res.data);
      return userInfo;
    }
    return {};
  } catch (error) {
    console.log(error);
    return {};
  }
};

// 获取所有用户信息
export const getAllUserInfoApi = async () => {
  try {
    const res = await http.get("/user/info/get/all");
    if (res && res.code === 200 && res.data) {
      const userInfo = JSON.parse(res.data);
      return userInfo;
    }
    return [];
  } catch (error) {
    console.log(error);
    return [];
  }
};

// 添加用户
export const addUserApi = async (data) => {
  try {
    await http.post("/user/add", data);
    return true;
  } catch (error) {
    console.log(error);
    return false;
  }
};

// 编辑用户
export const editUserApi = async (data) => {
  try {
    await http.post("/user/info/update", data);
    return true;
  } catch (error) {
    console.log(error);
    return false;
  }
};

// 删除用户
export const deleteUserApi = async (data) => {
  try {
    await http.post("/user/delete", data);
    return true;
  } catch (error) {
    console.log(error);
    return false;
  }
};

// 登录
export const loginApi = async (params) => {
  try {
    const res = await http.get("/login", { params });
    if (res && res.code === 200 && res.data) {
      return JSON.parse(res.data);
    }
    return {};
  } catch (error) {
    console.log(error);
    return {};
  }
};
